﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Runda.CMS.Dal
{
    public partial class SysWebpartsDal
    {
        public DataTable getPublicWebPart() {          
            string sql = @"SELECT CASE WHEN w2.Expanded IS NULL THEN 0 ELSE Expanded END AS Expanded,
 CASE WHEN w2.Area IS NULL THEN 'false' ELSE 'true' END AS checked,CASE WHEN 
 w2.WPName IS NULL THEN w1.WPName ELSE w2.WPName END AS WPName,CASE WHEN w2.Area IS NULL THEN '0' ELSE 
 w2.Area END AS Area,CASE WHEN w2.Sequence IS NULL THEN '0' ELSE w2.Sequence END AS Sequence,w1.WPCode,
 w1.WPType,w1.URL,w2.DesktopID,CASE WHEN (w2.Height IS NULL OR w2.Height='') THEN w1.Height ELSE 
 w2.Height END Height,w1.Comments FROM Cms_Sys_Webparts w1 LEFT JOIN Cms_Sys_DesktopWebParts w2 ON 
 (w1.WPCode=w2.WPCode AND w2.DesktopID = (select top 1 DesktopID from Cms_Sys_Desktop))    ORDER BY checked desc,Area,Sequence,w1.WPCode";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public DataTable getPrivateWebPart(Guid UserID) {
            DataTable dt = new DataTable();
            string sql = @" SELECT CASE WHEN w2.Expanded IS NULL THEN 0 ELSE Expanded END AS Expanded,
 CASE WHEN w2.Area IS NULL THEN 'false' ELSE 'true' END AS checked,CASE WHEN 
 w2.WPName IS NULL THEN w1.WPName ELSE w2.WPName END AS WPName,CASE WHEN w2.Area IS NULL THEN '0' ELSE 
 w2.Area END AS Area,CASE WHEN w2.Sequence IS NULL THEN '0' ELSE w2.Sequence END AS Sequence,w1.WPCode,
 w1.WPType,w1.URL,w2.DesktopID,CASE WHEN (w2.Height IS NULL OR w2.Height='') THEN w1.Height ELSE 
 w2.Height END Height,w1.Comments FROM Cms_Sys_Webparts w1 LEFT JOIN Cms_Sys_DesktopWebParts w2 ON 
 (w1.WPCode=w2.WPCode AND w2.DesktopID = (select top 1 DesktopID from Cms_Sys_Desktop 
 where Owner=@UserID or Owner is null order by 
 IsDefault desc,IsPublic asc))    ORDER BY checked desc,Area,Sequence,w1.WPCode";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@UserID", DbType.Guid, UserID);

            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }
    }
}
